/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 *
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package com.riaevolution.components {
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.core.UIComponent;

	[Event(name="toogleChange", type = "flash.events.Event")]

	[Style(name="lineColor", type = "uint", format = "Color")]

	/**
	 * Divider indicator is a small line with an arrow in the middle of it.
	 * Acts like the toogle ui componentes.
	 * The component looks like something.
	 *
	 * ----------^------------ when the width is greater than height.
	 * The same but vertical if the width is not specified.
	 *
	 *
	 **/
	public class DividerIndicator extends UIComponent {

		public function DividerIndicator() {
			super();
			addEventListener(MouseEvent.MOUSE_OVER, turnOn);
			addEventListener(MouseEvent.MOUSE_OUT, turnOn);
			addEventListener(MouseEvent.CLICK, handle_Click);
		}

		private function handle_Click(event:Event):void {
			toogle = !toogle;
		}
		private static var arrowSize:int = 5;

		private var isOn:Boolean = false;

		public var dividerColor:int = 0x222222;

		override protected function measure():void {
			super.measure();
			measuredHeight = arrowSize * 2;
			measuredWidth = arrowSize * 2;
		}

		/**
		 * highligth handler.
		 * @param event
		 */
		private function turnOn(event:Event):void {
			isOn = (event.type == MouseEvent.MOUSE_OVER);
			invalidateDisplayList();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			var col:int = Number(this.getStyle('lineColor'));
			if (col != 0) {
				dividerColor = col;
			}

			var drawVertical:Boolean = unscaledWidth <= measuredWidth;

			this.graphics.clear();
			//draw the background active area
			this.graphics.beginFill(dividerColor, 0);
			this.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			this.graphics.endFill();

			//draw the line horizontal
			this.graphics.beginFill(dividerColor, isOn ? 1 : .5);
			if (!drawVertical) {
				this.graphics.drawRect(0, unscaledHeight / 2, unscaledWidth, 2);
			} else {
				this.graphics.drawRect(unscaledWidth / 2, 0, 2, unscaledHeight);
			}
			this.graphics.endFill();

			//el arrow
			this.graphics.beginFill(dividerColor, isOn ? 1 : .5);

			var dpx:Number = toogle ? -arrowSize : arrowSize;

			if (!drawVertical) {
				this.graphics.moveTo(unscaledWidth / 2 - arrowSize, unscaledHeight / 2 + (toogle ? 0 : 2));
				this.graphics.lineTo(unscaledWidth / 2 + arrowSize, unscaledHeight / 2 + (toogle ? 0 : 2));
				this.graphics.lineTo(unscaledWidth / 2, (unscaledHeight / 2) + dpx + (toogle ? 0 : 2));
				this.graphics.lineTo(unscaledWidth / 2 - arrowSize, unscaledHeight / 2 + (toogle ? 0 : 2));
			} else {
				this.graphics.moveTo(unscaledWidth / 2 + (toogle ? 0 : 2), unscaledHeight / 2 - arrowSize);
				this.graphics.lineTo(unscaledWidth / 2 + (toogle ? 0 : 2), unscaledHeight / 2 + arrowSize);
				this.graphics.lineTo((unscaledWidth / 2) + dpx + (toogle ? 0 : 2), unscaledHeight / 2);
				this.graphics.lineTo(unscaledWidth / 2 + (toogle ? 0 : 2), unscaledHeight / 2 - arrowSize);
			}

			this.graphics.endFill();
		}

		private var _toogle:Boolean = false;

		[Bindable]
		/**
		 * Toogle the arrow indicator element.
		 *
		 * @param value Booleano
		 */
		public function set toogle(value:Boolean):void {
			_toogle = value;
			invalidateDisplayList();
			dispatchEvent(new Event("toogleChange"));
		}

		public function get toogle():Boolean {
			return _toogle;
		}

	}
}